package com.fernfx.xingtan.my.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseAdapter;
import com.fernfx.xingtan.my.entity.MyBackCardsListEntity;
import com.fernfx.xingtan.utils.OtherUtil;

import java.util.List;

/**
 * Created by admin on 2018/8/20.
 */

public class MyBackCardsListAdapter extends BaseAdapter {
    private List<MyBackCardsListEntity.ObjBean> cardsList;

    public MyBackCardsListAdapter(Context context, List datas) {
        super(context, datas);
        this.cardsList = datas;
    }

    public void setDatas(List datas){
        this.cardsList = datas;
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_my_back_card_item;
    }

    @Override
    public Holder initHolder(View container) {
        BackCardsListHolder holder = new BackCardsListHolder();
        holder.backNameTv = (TextView) container.findViewById(R.id.back_name_tv);
        holder.backCardTypeTv = container.findViewById(R.id.back_card_type_tv);
        holder.backCardIdTv = container.findViewById(R.id.back_card_id_tv);
        return holder;
    }

    @Override
    public void initItem(View view, Holder holder, int position) {
        MyBackCardsListEntity.ObjBean objBean = cardsList.get(position);
        //set name
        BackCardsListHolder backCardsListHolder = (BackCardsListHolder) holder;
        backCardsListHolder.backNameTv.setText(OtherUtil.checkEmptyDefault(objBean.getBankName()));
        backCardsListHolder.backCardIdTv.setText(formatBackCardId(objBean.getBankAccId()));
    }

    private String formatBackCardId(String backCardId) {
        if (TextUtils.isEmpty(backCardId)) {
            return "";
        }

        char[] backCardArray = backCardId.toCharArray();
        //银行卡号item
        StringBuilder backCardsBuilder = new StringBuilder();
        for (int i = 0; i < backCardArray.length; i++) {
            if (i >= backCardArray.length - 4) {
                backCardsBuilder.append(backCardArray[i]);
            }else {
                backCardsBuilder.append("*");
            }
        }
        return backCardsBuilder.toString();
    }

    class BackCardsListHolder extends BaseAdapter.Holder {
        TextView backNameTv;

        TextView backCardTypeTv;
        //银行卡id
        TextView backCardIdTv;
    }
}
